﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Win32;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Drawing.Text;
using clEntidad.Ex;
using System.IO;
using System.Security.Cryptography;

namespace portero_
{
    public class Varios
    {
        [DllImport("user32.dll", EntryPoint = "SendMessage")]
        public static extern int SendMessage(int hWnd, uint Msg, int wParam, int lParam);

        [DllImport("avicap32.dll", EntryPoint = "capCreateCaptureWindowA")]
        public static extern int capCreateCaptureWindowA(string lpszWindowName, int dwStyle, int X, int Y, int nWidth, int nHeight, int hwndParent, int nID);

        [DllImport("user32.dll", EntryPoint = "EmptyClipboard")]
        public static extern int EmptyClipboard();

        [DllImport("avicap32.dll", EntryPoint = "capGetDriverDescriptionA")]
        //public static extern bool capGetDriverDescriptionA(int wdriver,string lpszName,int cbName,string lpszVer,int cbVer);
        public static extern bool capGetDriverDescriptionA(int wDriverIndex, [MarshalAs(UnmanagedType.VBByRefStr)]ref String lpszName, int cbName, [MarshalAs(UnmanagedType.VBByRefStr)] ref String lpszVer, int cbVer);


        public const int WM_USER = 1024;
        public const int WM_CAP_CONNECT = 1034;
        public const int WM_CAP_DISCONNECT = 1035;
        public const int WM_CAP_GET_FRAME = 1084;  // Constantes necesarias
        public const int WM_CAP_COPY = 1054;
        public const int WM_CAP_START = WM_USER;
        public const int WM_CAP_SET_PREVIEW = WM_CAP_START + 50;
        public const int WM_CAP_SET_PREVIEWRATE = WM_USER + 52;
        public static int CamHandle = 0; // Handle del dispositivo
        public static byte[] getKeyByte(String key_val, String cad_reg)
        {
            
            RegistryKey key = Registry.LocalMachine.OpenSubKey(cad_reg);
            if (key == null)
                throw new REGException("No existe registro " + Registry.LocalMachine.ToString() + "\\" + cad_reg);
            byte[] rpta = (byte[]) key.GetValue(key_val);
            return rpta; 
        }

        public static String getKey(String key_val, String cad_reg)
        {
            RegistryKey key = Registry.LocalMachine.OpenSubKey(cad_reg);
            if (key == null)
                throw new REGException("No existe registro " + Registry.LocalMachine.ToString() + "\\" + cad_reg);
            Object rpta = key.GetValue(key_val);
            if (rpta == null)
                throw new REGException("No existe clave " + Registry.LocalMachine.ToString() + "\\" + cad_reg + "\\" + key_val);
            return rpta.ToString();
        }

        public static Font getFontCodBar()
        {
            PrivateFontCollection pfc = new PrivateFontCollection();
            pfc.AddFontFile("free3of9.ttf");
            FontFamily family = new FontFamily("Free 3 of 9", pfc);
            return  new Font(family, 50);
        }
        public static byte[] hexToByte(String cadenaHex, String separador)
        {
            if (separador != "")
            {
                String[] sByte = cadenaHex.Split(separador.ToCharArray());
                byte[] bbyte = new byte[sByte.Length];
                for (int ix = 0; ix < sByte.Length; ix++)
                {
                    bbyte[ix] = byte.Parse(sByte[ix], System.Globalization.NumberStyles.HexNumber);
                }
                return bbyte;
            }
            return null;
        }
    }
}
